package view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.User;


/**
 * Servlet implementation class ServerServlet
 */

public class AuthenticationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuthenticationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        
        String withoutLast = request.getRequestURI().substring(0,request.getRequestURI().lastIndexOf("/"));
        String userID = withoutLast.substring(withoutLast.lastIndexOf("/")+1);
        String password = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
        
        PrintWriter out = response.getWriter();
        
        User user = new GetUserCommand().action(userID);
        
        if (user==null)
        	out.println("user not exist");
        else
        {	   
	        if (user.getPassword().equals(password))
	        	out.println("true");
	        else
	        	out.println("wrong password");
        }
        
        out.close();
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
       
	}

}
